package com.vitabuybuy.tv.ui.mine.talent.live

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityLiveNoticeDetailBinding
import com.vitabuybuy.tv.databinding.ActivityLiveReplayManageBinding
import com.library.common.base.BaseActivity
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.talent.live.adapter.LiveReplayManageListAdapter
import com.vitabuybuy.tv.ui.mine.talent.live.viewmodel.LiveManageViewModel
import kotlinx.android.synthetic.main.activity_live_replay_manage.*

class LiveReplayManageActivity :
    BaseActivity<LiveManageViewModel, ActivityLiveReplayManageBinding>() {

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_live_replay_manage

    private val mAdapter by lazy { LiveReplayManageListAdapter() }
    private var pageNumber = 1
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
        setTitle(getString(R.string.live_replay_manage))
//        mTvRight.apply {
//            text = getString(R.string.save)
//            visibility = View.VISIBLE
//        }
        listview.apply {
            layoutManager = LinearLayoutManager(mContext)
            adapter = mAdapter
            isNestedScrollingEnabled = false
        }
    }

    override fun observeData() {
        viewModel.apply {
            getPlayBackManageList(false, pageNumber)
            liveManageList.observe(this@LiveReplayManageActivity, Observer {
                if (pageNumber == 1) {
                    mAdapter.setNewData(it)
                } else {
                    mAdapter.addData(it)
                }
                if (mAdapter.mData.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
            })
        }
    }

    override fun eventHandling() {
        mAdapter.onDeletePlayBackListener = {
            AlertDialog.Builder(mContext)
                .setTitle(getString(R.string.reminder))
                .setMessage(getString(R.string.are_you_sure_delete_live_play))
                .setCancelable(true)
                .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                    viewModel.deleteLivePlayBack(mAdapter.mData[it].id, it)
                }
                .setNegativeButton(getString(R.string.cancel), null).create().show()
        }
        mAdapter.onIsShowHomePageBackListener = {
            viewModel.isShowHome(mAdapter.mData[it].isShow, mAdapter.mData[it].id, it)
        }
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getPlayBackManageList(false, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getPlayBackManageList(false, pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getPlayBackManageList(false, pageNumber)
            }
            1 -> refresh.onLoad(msg.arg1)
            2 -> {
                mAdapter.removeAt(msg.arg1)
                XToast.success(getString(R.string.already_delete))
            }
            3 -> {
                mAdapter.mData[msg.arg1].isShow = !mAdapter.mData[msg.arg1].isShow
                mAdapter.notifyItemChanged(msg.arg1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, LiveReplayManageActivity::class.java)
        }
    }
}
